var $;
var layer;
var table;
var form;
var upload;

    layui.use(['layer', 'table', 'form','upload'], function () {
        layer = layui.layer;
        $ = layui.jquery;
        table = layui.table;
        form = layui.form;
        upload=layui.upload;

        getTab();

        //初始化表格
        function getTab() {
            var id=$("#courseId").val();
            $.ajax({
                url:"/tea/selcourse",
                type:"post",
                dataType:"json",
                data:{"courseId":id},
                success:function(data){
                    if(data.code>-1){
                        $("#courseName").val(data.obj.courseName);//课程名称
                        var createDate=settime(data.obj.createDate);
                        $("#createDate").val(createDate);//上传时间
                        $("#createName").val(data.obj.createName);//上传人
                        $("#courseNprice").val(data.obj.courseNprice);//课程售价
                        $("#coursePrice").val(data.obj.courseNprice);//课程原售价
                        $("#coursePrimary").val(data.obj.coursePrimary);//课程关键字
                        $("#className").val(data.obj.className);//课程分类
                        /*$("#likeNum").val(data.obj.likeNum);//点赞量
                        $("#collectNum").val(data.obj.collectNum);//收藏量*/
                        if(data.obj.likeNum){
                            $("#likeNum").val(data.obj.likeNum);//点赞量
                        }else{
                            $("#likeNum").val(0);
                        }
                        if(data.obj.collectNum){
                            $("#collectNum").val(data.obj.collectNum);//收藏量
                        }else{
                            $("#collectNum").val(0);
                        }
                        $("#commentNum").val(data.obj.commentNum);//评论量
                        $("#courseIntro").val(data.obj.courseIntro);//课程介绍
                        $("#img").attr("src",data.obj.url);//课程封面图url
                    }
                },
                error: function () {
                    layer.msg('数据出错！');
                }
            });
        };

        /*购置日期转换*/
        function settime(da){
            var date=new Date(da);
            var year=date.getFullYear();
            var month=date.getMonth()+1;
            var day=date.getDate();
            var hours=date.getHours();
            var mm=date.getMinutes();
            var s=date.getSeconds();
            return year+'-'+add(month)+'-'+add(day)+' '+add(hours)+':'+add(mm)+':'+add(s);
            //return year+'-'+month+'-'+day+' '+hours+':'+mm+':'+s;
        };
        function add(time){//给非整数添0
            return time<10?'0'+time:time;
        };


        //设置课程分类
        $("#setcourseCatory").on("click",function () {
            layer.open({
                type:2,
                title:"课程分类",
                area:['300px','300px'],
                shade:0.4,
                offset:'auto',
                content:"TeaCourseClass.html",
                success:function(layero,index) {},
                btn:['确定','取消'],
                yes:function(index,layero){
                    var obj=layero.find('iframe').contents();
                    var classId=obj.find("#classId").val();//课程分类
                    $("#classId").val(classId);
                    $.post("/tea/selcourseClass", {classId: classId}, function (res) {
                        if(res){
                            $("#className").val(res.obj);
                            layer.close(index);
                        }else{
                            layer.msg("数据加载失败",{icon:2});
                        }
                    });
                },
                btn2:function (index,layero) {
                    layer.close(index);
                }
            });
        });

        //上传图片
        var uploadInst = upload.render({
            elem: '#setimgbut'
            ,accept:'images'
            ,size: 5000 //限制文件大小，单位 KB
            , url: '/tea/addCourseImg' //上传接口
            , before: function (obj) {//预读本地文件示例
                obj.preview(function(index, file, result){
                    $('#img').attr('src', result); //图片链接（base64）
                });
            },
            done:function(res){//上传完成的回调
                if(res.code>-1){
                    $("#url").val(res.obj);
                    layer.msg('上传完毕', {icon: 1});
                }else{
                    layer.msg('上传失败,请重试', {icon: 2});
                }
            },
            error:function(){//请求异常回调
                layer.msg('上传出错，请重试', {icon: 2});
            }
        });

        //提交修改
        $("#courseBtn").on("click",function () {
            var formdom=new FormData(document.getElementById("formId"));//获取dom对象
            $.ajax({
                url: "/tea/updCourseEdit",
                type: "post",
                dataType: "json",
                data: formdom,
                contentType:false,//不转换为其他格式，是JS的form表单对象
                processData:false,
                success: function (data) {
                    if (data.code>-1) {
                        //关闭窗口	layer.close(index)关闭特定层
                        layer.msg("课程修改成功", {icon: 1}, function(){
                            var layer_2=parent.layer_2;
                            layer_2.close(layer_2.index);
                        });
                    } else {
                        layer.msg("数据加载失败", {icon: 2});
                    }
                }
            });
        });

        //取消按钮
        $("#cancelBtn").on("click",function () {
            var layer_2=parent.layer_2;
            layer_2.close(layer_2.index);
        });
    });